26. Exercise: Static Methods

Exercise: Static Method

In addition to static member variables, C++ supports static member functions (or "methods"). Just like static member variables, static member functions are instance-independent: they belong to the class, not to any particular instance of the class.

One corollary to this is that we can method invoke a static member function without ever creating an instance of the class.

You will try just that in this exercise.

Instructions

  1. Refactor class Sphere to move the volume calculation into a static function.
  2. Verify that the class still functions as intended.
  3. Make that static function public.
  4. Call that static function directly from main() to calculate the hypothetical volume of a sphere you have not yet instantiated.

Static2

Workspace

This section contains either a workspace (it can be a Jupyter Notebook workspace or an online code editor work space, etc.) and it cannot be automatically downloaded to be generated here. Please access the classroom with your account and manually download the workspace to your local machine. Note that for some courses, Udacity upload the workspace files onto https://github.com/udacity, so you may be able to download them there.

Workspace Information:

  • Default file path:
  • Workspace type: jupyter
  • Opened files (when workspace is loaded): n/a